// 入口文件
import Vue from 'vue';
// 组件
import app from './app.vue';
// mui
import 'mint-ui/lib/style.css';
import '../lib/mui/css/mui.css';
import '../lib/mui/css/icons-extra.css';
//路由
import VueRouter from 'vue-router';
Vue.use(VueRouter);
import router from './router.js'
//axios
import Axios from 'axios';
Axios.defaults.baseURL = "http://www.liulongbin.top:3005/";
Vue.prototype.$http = Axios
//Vuex状态管理
import Vuex from 'vuex';
Vue.use(Vuex);
var carList = JSON.parse(localStorage.getItem('car')) || [];
//图片预览插件
import VuePreview from 'vue-preview';
Vue.use(VuePreview);
//时间格式化
import moment from 'moment';
Vue.filter('dateFromt', (dateStr, pattern = 'YYYY-MM-DD HH:mm:ss') => {
    return moment(dateStr).format(pattern);
})
//按需组件
import { Header, Swipe, SwipeItem, Button, Lazyload, Switch } from 'mint-ui';
Vue.component(Header.name, Header);
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Button.name, Button);
Vue.use(Lazyload); //懒加载
Vue.component(Switch.name, Switch);
//vant 按需导入
import 'vant/lib/index.css'
import { Tabbar, TabbarItem } from 'vant';
Vue.use(Tabbar).use(TabbarItem);

const store = new Vuex.Store({
    state: {
        car: carList
    },
    mutations: {
        getGoodsInfoList: (state, goodsinfo) => {
            let flag = state.car.some(item => {
                if (item.id == goodsinfo.id) {
                    item.count += goodsinfo.count;
                    return true;
                }
            })
            if (!flag) {
                state.car.push(goodsinfo);
            }
            localStorage.setItem('car', JSON.stringify(state.car));
        },
        updateGoodsCount(state, goodsCount) {
            state.car.some(item => {
                if (item.id == goodsCount.id) {
                    item.count = parseInt(goodsCount.count)
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car));
        },
        remoteGoodsInfo(state, id) {
            state.car.some((item, index) => {
                if (item.id == id) {
                    state.car.splice(index, 1)
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car));
        },
        updataGoodsSelected(state, info) {
            state.car.some(item => {
                if (item.id == info.id) {
                    item.selected = info.selected;
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car));
        }
    },
    getters: {
        getAllCount: (state) => { //微章的数量
            let countAll = 0;
            state.car.forEach(item => {
                countAll += item.count
            })
            return countAll;
        },
        getCarCount(state) {
            let obj = {};
            state.car.forEach(item => {
                obj[item.id] = item.count
            })
            return obj;
        },
        getGoodsSelected(state) {
            let obj = {};
            state.car.forEach(item => {
                obj[item.id] = item.selected
            })
            return obj;
        },
        getGoodsCountAndTotal(state) {
            let obj = {
                count: 0,
                total: 0
            }
            state.car.forEach(item => {
                if (item.selected) {
                    obj.count += item.count;
                    obj.total += item.count * item.price
                }
            })
            return obj;
        }
    }
})
new Vue({
    el: '#app',
    render: c => c(app),
    router, //挂载路由
    store   //挂载状态管理
})
